#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define High 15
#define Width 20 

int ball_x,ball_y;
int ball_vx,ball_vy;
int canvas[High][Width]={0};

void gotoxy(int x,int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
}

void startup()
{
	ball_x=High-1;
	ball_y=Width/2;
	ball_vx=-1;
	ball_vy=1;
	canvas[ball_x][ball_y]=1;
	
	int i,k;
	for(k=0;k<Width;k++){
		for(i=0;i<=High/4;i++){
			canvas[i][k]=3;
		}
	}
}

void show()
{
	gotoxy(0,0);
	
	int i,j;
	for(i=0;i<High;i++){
		for(j=0;j<Width;j++){
			if(canvas[i][j]==0){
				printf(" ");
			}
			else if(canvas[i][j]=1){
				printf("o");
			} 
			else if(canvas[i][j]=2){
				printf("O");
			}
		}
		printf("|\n");
	}
	for(j=0;j<Width;j++){
		printf("-");
	}
	printf("\n");
}

void updateWithoutInput()
{
	static int speed=0;
	if(speed<3){
		speed++;
	}
	if(speed==3){
		speed=0;
		
		canvas[ball_x][ball_y]=0;
		ball_x=ball_x+ball_vx;
	    ball_y=ball_y+ball_vy;
		canvas[ball_x][ball_y]=1;
		
		if((ball_x==0)||(ball_x==High-1)){
		    ball_vx=-ball_vx;
	    }
	    if((ball_y==0)||(ball_y==Width-1)){
		    ball_vy=-ball_vy;
	    }
	    
	    if(canvas[ball_x-1][ball_y]==3){
	    	ball_vx=-ball_vx;
	    	canvas[ball_x-1][ball_y]==0;
	    	printf("\a");
		}
	}
}

void updateWithInput()
{
}

int main()
{
	startup();
	while(1){
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}
